//
//  HCUntreatedCallViewController.m
//  HelloCall
//
//  Created by WangYongpeng on 13-9-21.
//  Copyright (c) 2013年 HC. All rights reserved.
//

#import "HCCallOperatingViewController.h"
#import "HCComMenuCell.h"
#import "HCCallInfoViewController.h"
#import "HCRemarkListViewController.h"
#import "HCRemarkInfoViewController.h"
#import "HCCallListModel.h"
#import "HCCallPhonePopupView.h"
#import "HCCallRecordsModel.h"
#import "HCPickerView.h"
#import "HCMenuview.h"
#import "HCMainViewController.h"
#import "HCNetworkManger.h"

extern HCMainViewController* g_mainViewController;

@interface HCCallOperatingViewController ()<UITableViewDataSource, UITableViewDelegate, HCComMenuCellDelegate, HCRemarkListViewControllerDelegate, HCPickerViewDelegate, HCMenuviewDelegate, HCModelDataObserver>
{
    ECallOperatingType  m_operatingType;
}
@property (nonatomic, retain)UITextView*    textView;
@property (nonatomic, retain)HCMenuview*    menuView;
@property (nonatomic, retain)UIActivityIndicatorView* activityIndicatorView;

@end

@implementation HCCallOperatingViewController

@synthesize textView = m_textView;
@synthesize delegate = m_delegate;
@synthesize menuView = m_menuView;
@synthesize activityIndicatorView = m_activityIndicatorView;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    UIBarButtonItem* menuItem = [[[UIBarButtonItem alloc] initWithTitle:@"菜单" style:UIBarButtonItemStylePlain target:self action:@selector(menuItemPress:)] autorelease];
    self.navigationItem.leftBarButtonItem = menuItem;
    
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height - 80)];
    self.textView.textColor = [UIColor blackColor];
    self.textView.hidden = YES;
    self.textView.editable = NO;
    [self.view addSubview:self.textView];
    
    m_operatingType = ECallOperatingType_Untreated;
    [self setCallOperatingType:m_operatingType];
    
    NSMutableArray* menuArray = [NSMutableArray arrayWithCapacity:4];
    menuDataNode* node = [[[menuDataNode alloc] init] autorelease];
    node.title = @"未处理话单";
    node.menuId = ECallOperatingType_Untreated;
    [menuArray addObject:node];
    
    node = [[[menuDataNode alloc] init] autorelease];
    node.title = @"放弃沟通话单";
    node.menuId = ECallOperatingType_GiveUp;
    [menuArray addObject:node];
    
    node = [[[menuDataNode alloc] init] autorelease];
    node.title = @"申请/被转移话单";
    node.menuId = ECallOperatingType_Shift;
    [menuArray addObject:node];
    
    node = [[[menuDataNode alloc] init] autorelease];
    node.title = @"话单统计";
    node.menuId = ECallOperatingType_Statistics;
    [menuArray addObject:node];
    
    self.menuView = [[[HCMenuview alloc] initWithFrame:CGRectMake(0, 0, 200, [[UIScreen mainScreen] bounds].size.height)] autorelease];
    self.menuView.delegate = self;
    self.menuView.menuDateArray = menuArray;
    [g_mainViewController setMenuView:self.menuView];
    
    [[HCCallListModel defaultModel] addObserver:self];
}

- (void)dealloc
{
    [[HCCallListModel defaultModel] removeObserver:self];
    
    self.textView = nil;
    self.menuView = nil;
    self.activityIndicatorView = nil;
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)showAllCallInfo
{
    NSString* text = @"查看总详情内容：\n\n";
    
    text = [text stringByAppendingFormat:@"本机号码：%@\n",[HCCallListModel defaultModel].selfPhoneNumber];
    text = [text stringByAppendingFormat:@"总数：%d\n",[[HCCallListModel defaultModel] getAllCallInfoCount]];
    text = [text stringByAppendingFormat:@"推广来源数：%d\n",[[HCCallListModel defaultModel] getCallInfoCountWithSourceType:CallSourceType_spread]];
    text = [text stringByAppendingFormat:@"企业分配数：%d\n",[[HCCallListModel defaultModel] getCallInfoCountWithSourceType:CallSourceType_allocation]];
    text = [text stringByAppendingFormat:@"定时回访数：%d\n",[[HCCallListModel defaultModel] getCallInfoCountWithSourceType:CallSourceType_visit]];
    
    self.textView.text = text;
}

- (void)setCallOperatingType:(ECallOperatingType)operatingType
{
    self.textView.hidden = YES;
    self.tableView.hidden = NO;
    
    CallInfoType infoType = CallInfoType_superCRM;
    
    switch (operatingType)
    {
        case ECallOperatingType_Statistics:
        {
            [[HCNetworkManger shareManger] getQueryTicketStatByTicketType:CallInfoType_untreated];
            
            self.title = @"话单统计";
            self.textView.hidden = NO;
            self.tableView.hidden = YES;
            [self showAllCallInfo];
            break;
        }
        case ECallOperatingType_Untreated:
        {
            infoType = CallInfoType_untreated;
            self.title = @"未处理话单";
            break;
        }
        case ECallOperatingType_GiveUp:
        {
            infoType = CallInfoType_giveUp;
            self.title = @"放弃沟通话单";
            break;
        }
        case ECallOperatingType_Shift:
        {
            infoType = CallInfoType_shift;
            self.title = @"申请/被转移话单";
            break;
        }
        default:
        {
            self.title = @"未处理话单";
            break;
        }
    }
    if ([self getCallInfoArray].count > 0)
    {
        [[HCNetworkManger shareManger] getQueryTicketListByTicketType:infoType offset:0 limit:[self getCallInfoArray].count];
    }
    else
    {
        [[HCNetworkManger shareManger] getQueryTicketListByTicketType:infoType offset:0];
    }
    
    m_activityIndicatorView = [[UIActivityIndicatorView alloc]
                                                  initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    [self.activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [self.view addSubview:self.activityIndicatorView];
    [self.activityIndicatorView startAnimating];
}

- (void)menuItemPress:(UIBarButtonItem*)button
{
    g_mainViewController.menuView = self.menuView;
    [g_mainViewController showOrCloseMenu];
    
//    //展开侧滑菜单
//    switch (m_operatingType)
//    {
//        case ECallOperatingType_Shift:
//            m_operatingType = ECallOperatingType_Statistics;
//            break;
//        case ECallOperatingType_Statistics:
//            m_operatingType = ECallOperatingType_Untreated;
//            break;
//        case ECallOperatingType_Untreated:
//            m_operatingType = ECallOperatingType_GiveUp;
//            break;
//        case ECallOperatingType_GiveUp:
//            m_operatingType = ECallOperatingType_Shift;
//            break;
//        default:
//            break;
//    }
////    m_operatingType = ECallOperatingType_Untreated;
//    [self setCallOperatingType:m_operatingType];
//    [self.tableView reloadData];
}

- (NSArray*)getCallInfoArray
{
    NSArray* array = nil;
    
    switch (m_operatingType)
    {
        case ECallOperatingType_Untreated:
        {
            array = [[HCCallListModel defaultModel] getCallInfoArrayWithType:CallInfoType_untreated];
            break;
        }
        case ECallOperatingType_GiveUp:
        {
            array = [[HCCallListModel defaultModel] getCallInfoArrayWithType:CallInfoType_giveUp];
            break;
        }
        case ECallOperatingType_Shift:
        {
            array = [[HCCallListModel defaultModel] getCallInfoArrayWithType:CallInfoType_shift];
            break;
        }
        default:
            break;
    }
    
    return array;
}

- (NSArray*)getDataArray
{
    return [self getCallInfoArray];
}

#pragma mark -- UITableViewDataSource
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = nil;
    if (m_menuIndex > -1 && indexPath.row == m_menuIndex)
    {
        NSString* key = @"MenuCell";
        cell = [tableView dequeueReusableCellWithIdentifier:key];
        if (nil == cell)
        {
            HCComMenuCell* menuCell = [[[HCComMenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:key] autorelease];
            menuCell.delegate = self;
            cell = menuCell;
        }
    }
    else
    {
        NSString* key = @"CallInfo";
        cell = [tableView dequeueReusableCellWithIdentifier:key];
        if (nil == cell)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:key] autorelease];
        }
        int index = indexPath.row;
        if (m_menuIndex > -1 && index > m_menuIndex)
        {
            index--;
        }
        HCCallInfo* callInfo = [[self getCallInfoArray] objectAtIndex:index];
        cell.textLabel.text = callInfo.phoneNumber;
        switch (callInfo.callSourceType)
        {
            case CallSourceType_allocation:
            {
                cell.detailTextLabel.text = @"来源：企业分配";
                break;
            }
            case CallSourceType_spread:
            {
                cell.detailTextLabel.text = @"来源：推广来源";
                break;
            }
            case CallSourceType_visit:
            {
                cell.detailTextLabel.text = @"来源：定时回访";
                break;
            }
            default:
            {
                cell.detailTextLabel.text = @"来源：企业分配";
                break;
            }
        }
    }
    return cell;
}

#pragma mark -- UITableViewDelegate
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    HCCallInfo* callInfo = [[self getCallInfoArray] objectAtIndex:indexPath.row];
//    HCCallPhonePopupView* view = [[HCCallPhonePopupView alloc] initByPhoneNumber:callInfo.phoneNumber contact:nil];
//    [self.view addSubview:view];
//    [view release];
//    
//    [[HCCallRecordsModel defaultModel] addCallRecord:callInfo.phoneNumber];
//}

#pragma mark -- HCCallInfoTableViewCellDelegate
- (void)showDetail
{
    if (m_menuIndex < 1)
    {
        return;
    }

    HCCallInfo* info = [[self getCallInfoArray] objectAtIndex:m_menuIndex - 1];
    [[HCNetworkManger shareManger] getQueryTicketDetailByTicketID:info.ticketID];
    
    m_activityIndicatorView = [[UIActivityIndicatorView alloc]
                                                  initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    [self.activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [self.view addSubview:self.activityIndicatorView];
    [self.activityIndicatorView startAnimating];
}

- (void)showRemark
{
    if (m_menuIndex < 1)
    {
        return;
    }
    
    HCRemarkListViewController* viewController = [[HCRemarkListViewController alloc] init];
    viewController.delegate = self;
    [viewController setCallInfo:[[self getCallInfoArray] objectAtIndex:m_menuIndex - 1]];
    
    [self showNextWindow:viewController];
}

- (void)changeState
{
    int currentSel = 0;
    HCCallInfo* callInfo = [[self getCallInfoArray] objectAtIndex:m_menuIndex - 1];
    
    switch (callInfo.callInfoType)
    {
        case CallInfoType_untreated:         //未处理
        case CallInfoType_superCRM:          //录入CRM
        {
            currentSel = 0;
            break;
        }
        case CallInfoType_giveUp:            //放弃
        {
            currentSel = 1;
            break;
        }
        case CallInfoType_shift:             //申请转移:
        {
            currentSel = 2;
            break;
        }
        default:
            break;
    }

    
    CGRect wBounds = [UIScreen mainScreen].bounds;
    HCPickerView *pickerView =  [[ [ HCPickerView alloc ] initWithFrame: wBounds] autorelease];  ///<修改：增加autorelease。（系统设置组合框选择）liuqina, 2013/01/08
    pickerView.delegate = self;
    [pickerView addSuperView:self.view data:[NSArray arrayWithObjects:@"录入CRM",@"放弃跟踪",@"申请转移", nil] currentSel:currentSel settingItemName:nil];
}

- (void)callPhone
{
    if (m_menuIndex < 1)
    {
        return;
    }
    
    HCCallInfo* callInfo = [[self getCallInfoArray] objectAtIndex:(m_menuIndex - 1)];
    HCCallPhonePopupView* view = [[HCCallPhonePopupView alloc] initByPhoneNumber:callInfo.phoneNumber contact:nil];
    [self.view addSubview:view];
    [view release];
    
    [[HCCallRecordsModel defaultModel] addCallRecord:callInfo.phoneNumber];
}

#pragma mark -- HCRemarkListViewControllerDelegate
- (void)showRemarkInfoDetail:(HCCallRemarkInfo *)remarkInfo
{
    HCRemarkInfoViewController* viewController = [[HCRemarkInfoViewController alloc] init];
    viewController.remarkInfo = remarkInfo;
    
    [self showNextWindow:viewController];
}

#pragma mark -- HCPickerViewDelegate
- (void)userSelectedIndex:(int)index settingItemName:(NSString*)itemName
{
    HCCallInfo* callInfo = [[self getCallInfoArray] objectAtIndex:m_menuIndex - 1];
    [[HCCallListModel defaultModel] changeCallInfoTypeByID:callInfo.ID callInfoType:(CallInfoType)(index + 1)];
    [self.tableView reloadData];
}

#pragma mark -- HCMenuviewDelegate
- (void)menuProcessByMenuID:(int)menuId
{
    m_operatingType = (ECallOperatingType)menuId;
    [self setCallOperatingType:m_operatingType];
    [self.tableView reloadData];
    
    [self menuItemPress:nil];
}


#pragma mark -- HCModelDataObserver
- (void)dataUpdateByMsgID:(int)msgID
{
    /*pause modify---------------------------------------
    switch (msgID)
    {
        case EQueryTicketList:
        {
            [self callInfoListDataUpdate];
            break;
        }
        case EQueryTicketDetail:
        {
            [self ticketDetailDataUpdate];
            break;
        }
        default:
        {
            break;
        }
    }
    *pause modify---------------------------------------*/
}

- (void)callInfoListDataUpdate
{
    [self.tableView reloadData];
    [self.activityIndicatorView stopAnimating];
    self.activityIndicatorView = nil;
}

- (void)ticketDetailDataUpdate
{
    HCCallInfoViewController* viewController = [[[HCCallInfoViewController alloc] init] autorelease];
    HCCallInfo* info = [[self getCallInfoArray] objectAtIndex:m_menuIndex - 1];
    [viewController showCallDetail:info];
    
    [self showNextWindow:viewController];
    
    [self.activityIndicatorView stopAnimating];
    self.activityIndicatorView = nil;
}

@end
